﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyNhaHang.GUI
{
    public partial class Login : Form
    {
        Bussiness.BusLogin busLogin;
        public Login()
        {
            InitializeComponent();
            busLogin = new Bussiness.BusLogin();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private String getMD5(String txt)
        {
            String str = "";

            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txt);
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            buffer = md5.ComputeHash(buffer);

            foreach (byte b in buffer)
            {
                str += b.ToString("X2");
            }
            return str;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            String password = getMD5(this.txtPass.Text);

            if (busLogin.checkIDAndPasswork(this.txtID.Text, password))
            {
                GUI.Main main;
                Thread t1 = new Thread(() =>
                {
                    main = new Main();
                    main.ShowDialog();
                });
                t1.Start();

                this.Close();
            }
            else
            {
                this.lblMessage.Text = "Tài khoản hoặc mật khẩu không đúng!!";
            }
        }
    }
}
